package com.egao.common.publicly.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.component.elasticsearch.service.ElasticSearchService;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.constants.AttachmentConstants;
import com.egao.common.module.company.entity.*;
import com.egao.common.module.company.service.*;
import com.egao.common.module.push.constants.PushConstants;
import com.egao.common.module.push.entity.PushMsg;
import com.egao.common.module.push.mapper.PushMsgMapper;
import com.egao.common.publicly.entity.MatchingVO;
import com.egao.common.publicly.entity.PubliclyCareerVO;
import com.egao.common.publicly.entity.ResumeButtonVO;
import com.egao.common.publicly.mapper.PubliclyMatchingMapper;
import com.egao.common.publicly.mapper.PubliclyPreachCloudMapper;
import com.egao.common.publicly.mapper.PubliclyPreachMapper;
import com.egao.common.publicly.mapper.PubliclyRecruitMapper;
import com.egao.common.publicly.service.PubliclyMatchingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 信息推送实现类
 * @author pyx
 */
@Service
public class PubliclyMatchingServiceImpl extends ServiceImpl<PubliclyMatchingMapper, PushMsg> implements PubliclyMatchingService {
    @Autowired
    private PositionService positionService;
    @Autowired
    private AttachmentService attachmentService;
    @Autowired
    private PushMsgMapper pushMsgMapper;
    @Autowired(required = false)
    private PubliclyRecruitMapper publiclyRecruitMapper;
    @Autowired(required = false)
    private PubliclyPreachMapper publiclyPreachMapper;
    @Autowired(required = false)
    private PubliclyPreachCloudMapper publiclyPreachCloudMapper;
    @Autowired
    private ElasticSearchService elasticSearchService;


    @Override
    public List<PubliclyCareerVO> listPage(PageParam<PubliclyCareerVO> page) {
        if(StrUtil.isBlank((String)page.getPageData().get("xh"))){
            return new ArrayList<>();
        }

        if("1".equals(page.getPageData().get("type"))){
            return baseMapper.listRecruit(page);
        }else if("2".equals(page.getPageData().get("type"))){
            return baseMapper.listPreach(page);
        }else{
            return baseMapper.listPreachCloud(page);
        }
    }

    @Override
    public List<Position> listPositionMating(PushMsg pushMsg) {
        List<Position> positionList = new ArrayList<>();

        if (!StrUtil.isBlank(pushMsg.getPositionMatching())) {
            String[] positions = pushMsg.getPositionMatching().split("\\|");
            for (String p : positions) {
                if (!StrUtil.isBlank(p) && p.contains(",")) {
                    String[] p1 = p.split(",");
                    Position position = positionService.getPositionByIds(p1[0]).get(0);
                    if (position == null) {
                        continue;
                    }
                    position.setMatching(p1[1]);
                    if ("0".equals(position.getState())) {

                        if (position.getSalaryType().equals("0") && position.getSalaryMin() != null && position.getSalaryMax() != null) {
                            position.setSalary(position.getSalaryMin() + "~" + position.getSalaryMax());
                        } else {
                            position.setSalary("面议");
                        }

                        positionList.add(position);
                    }
                }
            }
            positionList.sort(new Comparator<Position>() {
                @Override
                public int compare(Position o1, Position o2) {
                    return Integer.parseInt(o2.getMatching()) - Integer.parseInt(o1.getMatching());
                }
            });
        }

        return positionList;
    }


    @Override
    public JsonResult getId(Integer id) {
        PushMsg pushMsg = baseMapper.selectById(id);
        if(pushMsg == null){
            return JsonResult.error("未获取到详情信息");
        }
        MatchingVO matchingVO = new MatchingVO();

        switch (pushMsg.getType()){
            case "1":
                matchingVO = baseMapper.getRecruit(pushMsg.getBusinessId());

                //更新点击量
                Recruit recruit = new Recruit();
                recruit.setId(matchingVO.getId());
                recruit.setHits(matchingVO.getHits() + 1);
                publiclyRecruitMapper.updateById(recruit);
                break;
            case "2":
                matchingVO = baseMapper.getPreach(pushMsg.getBusinessId());

                //更新点击量
                Preach preach = new Preach();
                preach.setId(matchingVO.getId());
                preach.setHits(matchingVO.getHits() + 1);
                publiclyPreachMapper.updateById(preach);
                break;
            case "3":
                matchingVO = baseMapper.getPreachCloud(pushMsg.getBusinessId());

                //更新点击量
                PreachCloud preachCloud = new PreachCloud();
                preachCloud.setId(matchingVO.getId());
                preachCloud.setHits(matchingVO.getHits() + 1);
                publiclyPreachCloudMapper.updateById(preachCloud);
                break;
            default:
                return JsonResult.error("没有匹配信息");
        }

        matchingVO.setMatchingId(id);

        //插入附件集合
        List<Attachment> attachmentList = attachmentService.getAttachByBusinessId(matchingVO.getId(), AttachmentConstants.TYPE_PREACH);
        if (attachmentList.size() == 0) {
            matchingVO.setAttachmentList(null);
        } else {
            matchingVO.setAttachmentList(attachmentList);
        }

        if (matchingVO.getSalaryType().equals("0") && matchingVO.getSalaryMin() != null && matchingVO.getSalaryMax() != null) {
            matchingVO.setSalary(matchingVO.getSalaryMin() + "~" + matchingVO.getSalaryMax());
        } else {
            matchingVO.setSalary("面议");
        }

        //修改推送状态为已读
        if(PushConstants.NO.equals(pushMsg.getReadFlag())){
            pushMsg.setReadFlag(PushConstants.YES);
            pushMsgMapper.updateById(pushMsg);
            List<PushMsg> pushMsgList = new ArrayList<>();
            pushMsgList.add(pushMsg);
            elasticSearchService.savePushList(pushMsgList,pushMsg.getBusinessId(),pushMsg.getType());
        }


        //添加匹配信息
        List<Position> positionList = listPositionMating(pushMsg);
        if(positionList != null){
            matchingVO.setListPosition(positionList);
        }

        return resumeButton(matchingVO,pushMsg);
    }


    /**
     * 按钮处理
     * @param matchingVO
     * @param pushMsg
     * @return
     */
    private JsonResult resumeButton(MatchingVO matchingVO,PushMsg pushMsg) {

        ResumeButtonVO button = new ResumeButtonVO();
        button.setButtonState(0);
        button.setButtonText("投递简历");

        if(StrUtil.isBlank(pushMsg.getResumeFlag())){
            return JsonResult.ok().put("resumeButton", button).setData(matchingVO);
        }

        if (pushMsg.getResumeFlag().equals(PushConstants.YES)) {
            button.setButtonState(1);
            button.setButtonText(pushMsg.getResumeDate().toString());
            return JsonResult.ok().put("resumeButton", button).setData(matchingVO);
        }

        return JsonResult.ok().put("resumeButton", button).setData(matchingVO);
    }
}
